##################################################
## Combining Amazon Data for Additional Bonuses ##
##################################################

## updated: 5/8/2014

########################################
##      CALCULATING UNDERPAYMENT      ##
########################################

# Data from: http://www.dol.gov/whd/state/stateMinWageHis.htm

fedrate <- 7.25                             # national federal minimum wage
highstates <- 8.00                          # mean rate among states with higher minimums
highmin <- 10                               # higher rate

minutes <- 4                                # time in minutes of task, e.g. 4 minutes
livingw <- highmin/(60/minutes)             # living wage payment for single HIT
hitprice <- .25 							# price of HIT if already paid, e.g. .25
bonusamt <- livingw - hitprice				# amount of bonus to raise to living wage

########################################
##  CREATING BULK PAYMENT COMMANDS    ##
########################################

# http://research-tricks.blogspot.com/2012/07/bulk-bonuses-on-mturk.html
# updated http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkCLT/CLTReference_GrantBonusCommand.html

# Download your batch(es) from https://requester.mturk.com/manage

#batch <- read.csv("Batch_1234567_batch_results.csv")
dim(batch)

batch2 <- batch[,colnames(batch) %in% c("WorkerId", "AssignmentId")]
dim(batch2)

bonuser <- data.frame(batch2$WorkerId, bonusamt, batch2$AssignmentId, "\"Thanks\"")
colnames(bonuser) <- c("WorkerId", "Bonus", "AssignmentId", "Reason")

sum(as.numeric(as.character(bonuser$Bonus))) # how much this will cost

# Note that the command here requires you to have set your environmental variables to point "$MTURK_CMD_HOME" to the correct folder for your Amazon command line tools (downloaded from http://aws.amazon.com/developertools/694)

bonustext <- paste("$MTURK_CMD_HOME/bin/grantBonus.sh", "-workerid", tax.bonus[,1], "-amount", tax.bonus[,2], "-assignment", tax.bonus[,3], "-reason", tax.bonus[,4], sep=" ")

length(bonustext) 

setwd() # This should point to where your command line tools are.  
tobonus <- file("bulkpayment_SAMPLE.txt")
writeLines(bonustext, tobonus)
close(tobonus)

####################
##  NEXT STEPS    ##
####################

## To run this file as a shell script, you must:

# 1. Open the txt file and add "#!/usr/bin/env sh" to top of file
# 2. Save file as .sh not .txt (export/rename)
# 3. Change permissions to execute by running "chmod +x filename" in the Terminal.
# 4. Add a separate line "sleep 3s" after each line so you don't overwhelm Amazon's tools.
# 5. Run by typing "./filename.sh" into Terminal and hitting enter.


########################################
## CHECKING WHO WAS PAID AND HOW MUCH ##
########################################

# Download your recent transaction history https://requester.mturk.com/mturk/transactionhistory

#paid <- read.csv("Transactions_2014-05-07_to_2014-05-08.csv")
colnames(paid)
dim(paid)

paid <- paid[paid$Amount==bonusamt,]
dim(paid)
sum(paid$Amount)

notpaid <- which(batch2$WorkerId %in% paid$Recipient.ID==FALSE)
batch3 <- batch3[notpaid,]
dim(batch3)



